package ca.jsbr.as2js_lib.utils 
{
	import ca.jsbr.as2js_lib.event.EventDispatcher;
	import flash.events.Event;
	import js.element.HTMLElement;
	import js.window;
	/**
	 * ...
	 * @author jsbr [me@jsbr.ca]
	 */
	public class KeyManager extends EventDispatcher
	{
		
		static public const KEYUP:String = "keyup";
		static public const KEYDOWN:String = "keydown";
		static public const KEYPRESS:String = "keypress";
		
		static private var _instance:KeyManager;
		
		private var _tag:HTMLElement
		private var _ready:Boolean;
		
		public function KeyManager() 
		{
			this.setTagListner(window)
		}
		
		public function setTagListner(tag:*):void
		{
			if (_tag)
			{
				_tag.removeEventListener("keypress", onKeyPress);
				_tag.removeEventListener("keydown", onKeyDown);
				_tag.removeEventListener("keyup", onKeyUp);
			}
			_tag = tag;
			_tag.addEventListener("keypress", onKeyPress);
			_tag.addEventListener("keydown", onKeyDown);
			_tag.addEventListener("keyup", onKeyUp);
			_ready = true;
		}
		

		
		private function addKeyListner(eventType:String, key:String, listner:Function):void
		{
			
		}
		
		private function onKeyUp(e:Event):void 
		{
			dispatchEvent(e);
		}
		
		private function onKeyDown(e:Event):void 
		{
			dispatchEvent(e);
		}
		
		private function onKeyPress(e:Event):void 
		{
			dispatchEvent(e);
		}
		
		
		public function get ready():Boolean 
		{
			return _ready;
		}
		
		
		
		static public function getInstance():KeyManager 
		{
			if (!_instance)
				_instance = new KeyManager();
			return _instance;
		}
		
		
		
		
	}

}